const http = require('../../utils/util.js');
const Base64 = require('../../utils/base');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showCdkey: false, //控制兑换码弹窗显示隐藏
		window_height: 0,
		mine_img: '/pages/image/mine/banner.png',
		wechat_avatar: '/pages/image/mine/user_img.png',
		userInfo: '',
		user_token: '',
		userFind: {},
		moduleList: [{
			moduleImg: "../image/mine/mine_img_1.png",
			moduleName: "我的订单"
		}, {
			moduleImg: "../image/mine/mine_img_2.png",
			moduleName: "我的钱包"
		}, {
			moduleImg: "../image/mine/mine_img_5.png",
			moduleName: "活动码兑换"
		}, {
			moduleImg: "../image/mine/mine_img_6.png",
			moduleName: "美团优惠券"
		}, {
			moduleImg: "../image/mine/mine_img_7.png",
			moduleName: "帮助中心"
		}, {
			moduleImg: "../image/mine/mine_img_8.png",
			moduleName: "其他"
		}, {
			moduleImg: "../image/mine/mine_img_9.png",
			moduleName: "合作"
		}, {
			moduleImg: "../image/mine/mine_img_10.png",
			moduleName: "拥有一家自习室"
		},  {
			moduleImg: "../image/mine/mine_img_13.png",
			moduleName: "意见反馈"
		}],

	},

	login() {
		wx.login({
			success(res) {
				if (res.code) {
					wx.setStorageSync('code', res.code)
				} else {}
			}
		})
		const _this = this
		wx.getUserProfile({
			desc: '获取用户相关信息',
			success: function(obj) {
				let code = wx.getStorageSync('code')
				var data = {
					code: code,
					encryptedData: obj.encryptedData,
					iv: obj.iv,
				}
				http.post('wechat/login', Base64.encode(JSON.stringify(data))).then(res => {
					if (res.status == 1) {
						wx.setStorageSync('user_id', res.result.user_id)
						wx.setStorageSync('user_token', res.result.user_token)
						_this.setData({
							userInfo: wx.getStorageSync('user_info'),
							user_token: wx.getStorageSync('user_token')
						})
						_this.getUserFind()
						//如果有手机号
						if (res.result.phone) {
							wx.setStorageSync('phone', res.result.phone)
							wx.switchTab({
								url: '/pages/mine/mine'
							})
						} else {
							wx.navigateTo({
								url: '/pages/mine/getphone/getphone'
							})
						}
					}

				})
			},
			fail: function(obj) {}
		})
	},
	getUserFind() {
		let _this = this
		let user_token = wx.getStorageSync('user_token')
		if (!user_token) {
			_this.setData({
				userFind: {}
			})
			wx.showModal({
				title: '提示',
				content: '你还没有登陆，是否登录?',
				success(res) {
					if (res.confirm) {
						wx.clearStorageSync()
						_this.login()
					}
				}
			})
		} else {
			let data = {
				user_id: wx.getStorageSync('user_id'),
				user_token: wx.getStorageSync('user_token')
			}
			http.post('user/find', Base64.encode(JSON.stringify(data))).then(res => {
				if (res.status == 1) {
					_this.setData({
						userFind: res.result
					})
				}
			})
		}
	},




	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	onShow: function() {
		this.getHeight()
		this.getUserFind()
		this.setData({
			user_token: wx.getStorageSync('user_token'),
			userInfo: wx.getStorageSync('user_info')
		})
	},
	/**
	 * 优惠券
	 *  */
	num_card: function() {
		//判断用户是否登录
		if (!this.data.user_token) {
			wx.showToast({
				title: '请先登录再进行操作',
				icon: 'none'
			})
			return
		}
		wx.navigateTo({
			url: '/pages/mine/mywallet/mywallet'
		})
	},
	userInfoHandler: function() {
		//判断用户是否登录
		if (!this.data.user_token) {
			wx.showToast({
				title: '请先登录再进行操作',
				icon: 'none'
			})
			return
		}
		let phone=this.data.userFind.phone
		wx.navigateTo({
			url: '/pages/mine/editinformation/editinformation?phone='+phone
		})
	},
	balance: function() {
		//判断用户是否登录
		if (!this.data.user_token) {
			wx.showToast({
				title: '请先登录再进行操作',
				icon: 'none'
			})
			return
		}
		wx.navigateTo({
			url: '/pages/mine/mywallet/mywallet'
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},
	/**
	 * 功能模块
	 * @param {*} e 
	 */
	choosedModule: function(e) {
		let orderid = e.currentTarget.dataset.index
		if (orderid !== 4 && orderid !== 5 && orderid !== 6 && orderid !== 7 && orderid !== 8) {
			if (!this.data.user_token) {
				wx.showToast({
					title: '请先登录再进行操作',
					icon: 'none'
				})
				return
			}
		}

		//预约记录
		if (orderid == 0) {
			wx.navigateTo({
				url: '/pages/mine/record/seat/seat'
			})
		}
		//我的钱包
		if (orderid == 1) {
			wx.navigateTo({
				url: '/pages/mine/mywallet/mywallet'
			})
		}
		//套餐购买
		// if (orderid == 2) {
		// 	wx.switchTab({
		// 		url: '/pages/purchase/purchase',
		// 		success: (e) => {
		// 			var page = getCurrentPages().pop();
		// 			if (page == undefined || page == null) return;
		// 			page.onLoad();
		// 		}
		// 	})
		// }
		//金额充值
		// if (orderid == 3) {
		// 	getApp().globalData.type = 4
		// 	wx.switchTab({
		// 		url: '/pages/purchase/purchase',
		// 		success: (e) => {
		// 			var page = getCurrentPages().pop();
		// 			if (page == undefined || page == null) return;
		// 			page.onLoad();
		// 		}
		// 	})
		// }

		//团购兑换
		if (orderid == 2) {
			wx.navigateTo({
				url: '/pages/mine/groupbuy/groupbuy'
			})
		}
		//优惠券
		if (orderid == 3) {
			wx.navigateTo({
				url: '/pages/mine/coupon/coupon'
			})
		}
		//帮助中心
		if (orderid == 4) {
			wx.navigateTo({
				url: '/pages/mine/help/help'
			})
		}
		//其他
		if (orderid == 5) {
			wx.navigateTo({
				url: '/pages/mine/other/other'
			})
		}
		//合作
		if (orderid == 6) {
			wx.navigateTo({
				url: '/pages/mine/join/join'
			})
		}
		//拥有一家自习室
		if (orderid == 7) {
			wx.navigateTo({
				url: '/pages/mine/studyroom/studyroom'
			})
		}
		//兑换邀请码
		// if (orderid == 10) {
		// 	this.setData({
		// 		showCdkey: true
		// 	})
		// }
		//图集
		// if (orderid == 10) {
		// 	wx.navigateTo({
		// 		url: '/pages/mine/atlas/atlas'
		// 	})
		// }
		//意见反馈
		if (orderid == 8) {
			wx.navigateTo({
				url: '/pages/mine/feedback/feedback'
			})
		}
	},

	//获取手机高度
	getHeight() {
		var windowHeight = wx.getSystemInfoSync().windowHeight
		this.setData({
			window_height: windowHeight
		})
	},
	//隐藏兑换界面
	hideCdkey() {
		this.setData({
			showCdkey: false
		})
	},
	//input输入事件
	inputChange(e) {
		if (e.detail.value.length > 10) return
		let arr = e.detail.value.split('')
		this.setData({
			pwdVal: arr
		})
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
